package test.n00400;


import static utils.Tools.listOf;

public class Problem00500 {
    public static void main(String[] args) {
        println(2, 10);
        println(2.1, 3);
        println(2, -2);
    }

    public static void println(double x, int n) {
        Solution ss = new Solution();
        System.out.println(listOf(x, n) + "," + listOf(ss.myPow(x, n)));
    }

    public static class Solution {

        public double myPow(double x, int n) {
            return n >= 0 ? quickMul(x, n) : 1.0 / quickMul(x, -n);
        }

        public double quickMul(double x, int n) {
            double result = 1.0;
            double contribute = x;
            while (n > 0) {
                if (n % 2 == 1) {
                    result = result * contribute;
                }

                contribute = contribute * contribute;
                n /= 2;
            }
            return result;
        }
    }
}
